﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;

namespace JQMdotNet
{
    public static partial class JQM
    {
        #region headers
        public static IHtmlString RenderBeginHeader()
        {
            return RenderBeginHeader(null, false, false, new Dictionary<string,object>());
        }

        public static IHtmlString RenderBeginHeader(object htmlAttributes)
        {
            return RenderBeginHeader(null, false, false, htmlAttributes);
        }

        public static IHtmlString RenderBeginHeader(IDictionary<string, object> htmlAttributes)
        {
            return RenderBeginHeader(null, false, false, htmlAttributes);
        }

        public static IHtmlString RenderBeginHeader(string id, object htmlAttributes)
        {
            return RenderBeginHeader(id, false, false, htmlAttributes);
        }

        public static IHtmlString RenderBeginHeader(string id, bool Fixed, bool Html5, object htmlAttributes)
        {
            return RenderBeginHeader(id, Fixed, false, new RouteValueDictionary(htmlAttributes));
        }

        public static IHtmlString RenderBeginHeader(string id, bool Fixed, bool Html5, IDictionary<string, object> htmlAttributes)
        {
            return RenderBeginToolBar("header", id, Fixed, Html5, htmlAttributes);
        }
        public static UniversalTag BeginHeader(this HtmlHelper helper)
        {
            return helper.BeginToolBar("header", null, false, false, new Dictionary<string, object>());
        }

        public static UniversalTag BeginHeader(this HtmlHelper helper, object htmlAttributes)
        {
            return helper.BeginHeader(null, false, false, htmlAttributes);
        }

        public static UniversalTag BeginHeader(this HtmlHelper helper, string id, object htmlAttributes)
        {
            return helper.BeginHeader(id, false, false, htmlAttributes);
        }


        public static UniversalTag BeginHeader(this HtmlHelper helper, string id, bool Fixed, bool Html5, object htmlAttributes)
        {
            return helper.BeginToolBar("header", id, Fixed, Html5, new RouteValueDictionary(htmlAttributes));
        }

        public static UniversalTag BeginHeader(this HtmlHelper helper, string id, bool Fixed, bool Html5, IDictionary<string, object> htmlAttributes)
        {
            return helper.BeginToolBar("header", id, Fixed, Html5, htmlAttributes);
        }

        #endregion

        

        #region Private Renderers
        private static IHtmlString RenderBeginToolBar(string role, string id, bool Fixed, bool Html5, IDictionary<string,object> htmlAttributes)
        {
            htmlAttributes.AddRole(role);
            if (!String.IsNullOrEmpty(id))
                htmlAttributes.ReplaceAttibute("id", id);
            if (Fixed)
                htmlAttributes.ReplaceAttibute("data-position", Fixed);
            if (Html5)
                return UniversalTagHelper.RenderBeginTag(role, htmlAttributes);
            else
                return UniversalTagHelper.RenderBeginTag("div", htmlAttributes);
        }

        private static UniversalTag BeginToolBar(this HtmlHelper helper, string role, string id, bool Fixed, bool Html5, IDictionary<string,object> htmlAttributes)
        {
            helper.ViewContext.Writer.Write(RenderBeginToolBar(role, id, Fixed, Html5, htmlAttributes));
            return new UniversalTag(helper,Html5?role:"div");
        }
        #endregion
    }
}